import { request } from '@/request';
import { useParams } from 'react-router-dom';


const useGroupEditor = () => {
   const { id } = useParams();
   const isNew = id === undefined;

   const initParams = {
      name: '',
      budget: 500,
      desc: '',
      members: [],
   };

   // 添加分组
   const addGroup = async (data) => {
      request('/groups', {
         method: 'POST',
         data,
         showSuccess: true,
         success: () => {
            window.history.back();
         },
      });
   };
   // 更新分组
   const updateGroup = async (data) => {
      request(`/groups/${id}`, {
         method: 'PATCH',
         data,
         showSuccess: true,
         success: () => {
            window.history.back();
         },
      });
   };

   return {
      addGroup,
      updateGroup,
      isNew,
      initParams,
      groupId: id
   };
};

export default useGroupEditor;
